home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / QuickDraw / Save PICT file / Template.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-24  |  2.5 KB  |  123 lines  |  [TEXT/KAHL]

  1.  
  2. #include <Files.h>
  3. #include <StandardFile.h>
  4.  
  5.  
  6.  
  7. PicHandle InitPicture(void);
  8. void PutPictToFile(PicHandle thePicture);
  9.  
  10. /*------ main ----------------------------------------------------------------------------*/
  11.  
  12. PicHandle ourPict;
  13.  
  14. main()
  15.  
  16. {
  17.  
  18. GrafPort myPort;
  19.  
  20.  
  21.     InitGraf((Ptr) &thePort);
  22.     OpenPort(&myPort);
  23.     InitFonts();
  24.     InitWindows();
  25.     InitMenus();
  26.     TEInit();
  27.     InitDialogs(nil);
  28.     InitCursor();
  29.  
  30.       ourPict = InitPicture();                       // get our picture
  31.       PutPictToFile(ourPict);                        // put it to a file
  32.       KillPicture(ourPict);                       // and then kill it
  33.  
  34. } /* main */
  35.  
  36.  
  37.  
  38. /*------ PutPictToFile ----------------------------------------------------------------------------*/
  39.  
  40.  
  41. void PutPictToFile(PicHandle thePicture)
  42.  
  43. {
  44.  
  45.     SFReply    tr;
  46.     short    rc, fRefNum, count;
  47.     long inOutCount;
  48.     Point    where;
  49.     unsigned char header[512];
  50.     OSErr myError;
  51.     
  52.     for (count = 0; count < 512; count++)
  53.         header[count] = 0x00;
  54.     
  55.     where.h=100; where.v=50;     /* where the  Standard File dialog window goes */
  56.     
  57.     SFPutFile( where, "\pSave PICT as:", "\pMy PICT File", NULL, &tr );
  58.     
  59.     if ( tr.good ) {
  60.         myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  61.         if (myError == dupFNErr) {
  62.             myError = FSDelete(tr.fName,tr.vRefNum);
  63.             myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  64.         }        /* this is quick 'n' dirty or there'd be more robust handling here */
  65.         
  66.         myError = FSOpen( tr.fName, tr.vRefNum, &fRefNum );
  67.         if ( myError == noErr ) { 
  68.             inOutCount = 512;
  69.             myError = FSWrite(fRefNum, &inOutCount, header);        /* write the header */
  70.             HLock((Handle)thePicture);
  71.             if (myError == noErr) {                    /* don't write if error the first time */
  72.                 inOutCount = GetHandleSize((Handle)thePicture);
  73.                 myError == FSWrite(fRefNum,&inOutCount,*thePicture);
  74.             }
  75.             FSClose( fRefNum );            /* close it */
  76.             HUnlock((Handle)thePicture);
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. /*------ InitPicture ----------------------------------------------------------------------*/
  84.  
  85. PicHandle InitPicture (void)
  86.  
  87. {
  88.     Rect myRect;
  89.     PicHandle thePicHandle;
  90.     Ptr theOldPictPtr, theNewPictPtr;
  91.     CGrafPort myPort;
  92.     long scrapResult;
  93.     unsigned long thePictureSize;
  94.     PixPatHandle thePixPat;
  95.     short theFont, textSize = 14;
  96.     
  97.     OpenCPort(&myPort);
  98.     SetRect(&myRect,0,0,200,200);
  99.     thePicHandle = OpenPicture(&myRect);
  100.     ClipRect(&myRect);
  101.     
  102.     thePixPat = GetPixPat(128);
  103.     
  104.     FillCOval(&myRect,thePixPat);
  105.     
  106.     MoveTo(22,22);
  107.     LineTo(55,55);
  108.     LineTo(58,22);
  109.     LineTo(22,58);
  110.     
  111.     GetFNum ("\pTimes", &theFont);
  112.     TextFont (theFont);
  113.     TextSize (textSize);
  114.     
  115.     DrawString("\pA wonderful test");
  116.     
  117.     ClosePicture();
  118.     CloseCPort(&myPort);
  119.     
  120.     return(thePicHandle);
  121.         
  122. }  /* InitPicture */
  123.